import {
    UserOutlined,
    SwitcherOutlined,
    SettingOutlined,
    CodepenOutlined,
    UsergroupAddOutlined,
    AuditOutlined,
    DollarOutlined,
    LineChartOutlined,
    EnvironmentOutlined,
    FormOutlined
} from '@ant-design/icons';
 let menu = [{
        key: '0-0',
        icon: <UserOutlined/> ,
        label: '系统首页',
        topath: "/home"

    },
    {
        key: '0-1',
        icon: <CodepenOutlined/> ,
        label: '轮播图管理',
        children: [{
                key: "0-1-0",
                label: "首页轮播图",
                topath: "/banner/home"
            },
            {
                key: "0-1-1",
                label: "活动页轮播图",
                topath: "/banner/active"
            }
        ]
    },
    {
        key: '0-2',
        icon: <SwitcherOutlined/> ,
        label: '产品管理',
        children: [{
                key: "0-2-0",
                label: "产品列表",
                topath: "/pro/list"
            },
            {
                key: "0-2-1",
                label: "筛选列表",
                topath: "/pro/search"
            }
        ]
    },
    {
        key: '0-3',
        icon: <UsergroupAddOutlined/> ,
        label: '账户管理',
        children: [{
                key: "0-3-0",
                label: "用户列表",
                topath: "/account/userlist"
            },
            {
                key: "0-3-1",
                label: "管理员列表",
                topath: "/account/adminlist"
            }
        ]
    },
    {
        key: '0-4',
        icon: <AuditOutlined/> ,
        label: '订单管理',
        children: [{
            key: "0-4-0",
            label: "订单列表",
            topath: "/orderfrom/orderfromlist"
        }]
    },
    {
        key: '0-5',
        icon: <DollarOutlined/> ,
        label: '购物车管理',
        children: [{
            key: "0-5-0",
            label: "购物车列表",
            topath: "/cart/list"
        }]
    },
    {
        label: '图表',
        icon: <LineChartOutlined/> ,
        key: '0-6',
        children: [{
                key: "0-6-0",
                label: "折线图",
                topath: "/echarts/line"
            },
            {
                key: "0-6-1",
                label: "饼图",
                topath: "/echarts/pie"
            }
        ]
    },
    {
        label: '地图',
        icon: <EnvironmentOutlined />,
        key: '0-7',
        topath: "/map"
    },
    {
        label: '富文本',
        icon: <FormOutlined />,
        key: '0-8',
        topath: "/richtext"
    },
    {
        label: '设置',
        icon: < SettingOutlined/> ,
        key: '0-9',
        topath: "/setting"
    }
]


export function filterAuth(quanxian) {
    if(quanxian === '*') {
        return menu
    }
    // 找出一级权限
    let yiji = []
    quanxian.forEach(item => {
        let index = item.indexOf('-')
        // console.log(11,index,item);
        let index2 = item.indexOf('-', index + 1)
        // console.log(22,index2,item);
        
        if(index2 >= 0) {
            yiji.push(item.slice(0, index2))
        }else if(index2==-1){
           yiji.push(item)
        }
    })
    // console.log(yiji);
    yiji = new Set(yiji)
    yiji = [...yiji]
    menu = menu.filter(item => yiji.findIndex(v => v === item.key) >= 0)
    // 此时的menu一级权限筛选成功
    // 定义所有二级权限组成的数组
    let erji = []
    quanxian.forEach(item => {
        let index = item.indexOf('-')
        let index2 = item.indexOf('-', index + 1)
        if(index2 >= 0) {
            erji.push(item)
        }
    })
    // 遍历menu中的每个children，从中找到所有在erji权限中的对象
    menu.forEach(item => {
        let brr = []
        if(item.children){
            item.children.forEach(v => {
                let index = erji.findIndex(vv => vv === v.key)
                if(index >= 0) {
                    brr.push(v)
                }
            })
            item.children = brr
        }
        // console.log(brr);
    })
    return menu
}